// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommunityToolkit.Tests;
using CommunityToolkit.WinUI.Controls;

namespace PrimitivesTests;

/// <summary>
/// These tests check for cases where we want to constrain the size of the ConstrainedBox with other
/// properties like MaxWidth and MaxHeight. The ConstrainedBox is greedy about using all the available
/// space, so Min properties don't really play the same factor? At least hard to create a practical test.
/// </summary>
public partial class Test_ConstrainedBox : VisualUITestBase
{
    [TestCategory("ConstrainedBox")]
    [TestMethod]
    public async Task Test_ConstrainedBox_Constrain_AspectMaxHeight()
    {
        await App.DispatcherQueue.EnqueueAsync(async () =>
        {
            var treeRoot = XamlReader.Load(@"<Page
    xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
    xmlns:controls=""using:CommunityToolkit.WinUI.Controls"">
    <Grid HorizontalAlignment=""Center"" VerticalAlignment=""Center"">
      <controls:ConstrainedBox x:Name=""ConstrainedBox"" AspectRatio=""2:1"" MaxHeight=""100"">
        <Border HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch"" Background=""Red""/>
      </controls:ConstrainedBox>
    </Grid>
</Page>") as FrameworkElement;

            Assert.IsNotNull(treeRoot, "Could not load XAML tree.");

            // Initialize Visual Tree
            await LoadTestContentAsync(treeRoot);

            var panel = treeRoot.FindChild("ConstrainedBox") as ConstrainedBox;

            Assert.IsNotNull(panel, "Could not find ConstrainedBox in tree.");

            // Force Layout calculations
            panel.UpdateLayout();

            var child = panel.Content as Border;

            Assert.IsNotNull(child, "Could not find inner Border");

            // Check Size
            Assert.AreEqual(200, child.ActualWidth, 0.01, "Actual width does not meet expected value of 200");
            Assert.AreEqual(100, child.ActualHeight, 0.01, "Actual height does not meet expected value of 100");
        });
    }

    [TestCategory("ConstrainedBox")]
    [TestMethod]
    public async Task Test_ConstrainedBox_Constrain_AspectMaxWidth()
    {
        await App.DispatcherQueue.EnqueueAsync(async () =>
        {
            var treeRoot = XamlReader.Load(@"<Page
    xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
    xmlns:controls=""using:CommunityToolkit.WinUI.Controls"">
    <Grid HorizontalAlignment=""Center"" VerticalAlignment=""Center"">
      <controls:ConstrainedBox x:Name=""ConstrainedBox"" AspectRatio=""2:1"" MaxWidth=""100"">
        <Border HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch"" Background=""Red""/>
      </controls:ConstrainedBox>
    </Grid>
</Page>") as FrameworkElement;

            Assert.IsNotNull(treeRoot, "Could not load XAML tree.");

            // Initialize Visual Tree
            await LoadTestContentAsync(treeRoot);

            var panel = treeRoot.FindChild("ConstrainedBox") as ConstrainedBox;

            Assert.IsNotNull(panel, "Could not find ConstrainedBox in tree.");

            // Force Layout calculations
            panel.UpdateLayout();

            var child = panel.Content as Border;

            Assert.IsNotNull(child, "Could not find inner Border");

            // Check Size
            Assert.AreEqual(100, child.ActualWidth, 0.01, "Actual width does not meet expected value of 100");
            Assert.AreEqual(50, child.ActualHeight, 0.01, "Actual height does not meet expected value of 50");
        });
    }

    [TestCategory("ConstrainedBox")]
    [TestMethod]
    public async Task Test_ConstrainedBox_Constrain_AspectBothMaxWidthHeight()
    {
        await App.DispatcherQueue.EnqueueAsync(async () =>
        {
            var treeRoot = XamlReader.Load(@"<Page
    xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
    xmlns:controls=""using:CommunityToolkit.WinUI.Controls"">
    <Grid HorizontalAlignment=""Center"" VerticalAlignment=""Center"">
      <controls:ConstrainedBox x:Name=""ConstrainedBox"" AspectRatio=""1:2"" MaxWidth=""200"" MaxHeight=""200"">
        <Border HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch"" Background=""Red""/>
      </controls:ConstrainedBox>
    </Grid>
</Page>") as FrameworkElement;

            Assert.IsNotNull(treeRoot, "Could not load XAML tree.");

            // Initialize Visual Tree
            await LoadTestContentAsync(treeRoot);

            var panel = treeRoot.FindChild("ConstrainedBox") as ConstrainedBox;

            Assert.IsNotNull(panel, "Could not find ConstrainedBox in tree.");

            // Force Layout calculations
            panel.UpdateLayout();

            var child = panel.Content as Border;

            Assert.IsNotNull(child, "Could not find inner Border");

            // Check Size
            Assert.AreEqual(100, child.ActualWidth, 0.01, "Actual width does not meet expected value of 100");
            Assert.AreEqual(200, child.ActualHeight, 0.01, "Actual height does not meet expected value of 200");
        });
    }
}
